package com.lxmes.sales.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;

import com.lxmes.sales.mapper.NcTransportNodeMapper;
import com.lxmes.sales.domain.NcTransportNode;
import com.lxmes.sales.service.INcTransportNodeService;

/**
 * 运输节点管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class NcTransportNodeServiceImpl implements INcTransportNodeService
{
    @Autowired
    private NcTransportNodeMapper ncTransportNodeMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询运输节点管理
     *
     * @param id 运输节点管理主键
     * @return 运输节点管理
     */
    @Override
    public NcTransportNode selectNcTransportNodeById(Long id)
    {
        return ncTransportNodeMapper.selectNcTransportNodeById(id);
    }

    /**
     * 查询运输节点管理列表
     *
     * @param ncTransportNode 运输节点管理
     * @return 运输节点管理
     */
    @Override
    public List<NcTransportNode> selectNcTransportNodeList(NcTransportNode ncTransportNode)
    {
        return ncTransportNodeMapper.selectNcTransportNodeList(ncTransportNode);
    }

    /**
     * 新增运输节点管理
     *
     * @param ncTransportNode 运输节点管理
     * @return 结果
     */
    @Override
    public int insertNcTransportNode(NcTransportNode ncTransportNode)
    {
        return ncTransportNodeMapper.insertNcTransportNode(ncTransportNode);
    }

    /**
     * 修改运输节点管理
     *
     * @param ncTransportNode 运输节点管理
     * @return 结果
     */
    @Override
    public int updateNcTransportNode(NcTransportNode ncTransportNode)
    {
        return ncTransportNodeMapper.updateNcTransportNode(ncTransportNode);
    }

    /**
     * 批量删除运输节点管理
     *
     * @param ids 需要删除的运输节点管理主键
     * @return 结果
     */
    @Override
    public int deleteNcTransportNodeByIds(Long[] ids)
    {
        return ncTransportNodeMapper.deleteNcTransportNodeByIds(ids);
    }

    /**
     * 删除运输节点管理信息
     *
     * @param id 运输节点管理主键
     * @return 结果
     */
    @Override
    public int deleteNcTransportNodeById(Long id)
    {
        return ncTransportNodeMapper.deleteNcTransportNodeById(id);
    }
    @Override
    public String imporIteminfo(List<NcTransportNode> ncTransportNodeList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncTransportNodeList) || ncTransportNodeList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcTransportNode ncTransportNode : ncTransportNodeList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncTransportNode);
                ncTransportNode.setCreateBy(operName);
                ncTransportNode.setUpdateBy(operName);
                this.insertNcTransportNode(ncTransportNode);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
